package com.yuanqi.service.impl;

import com.yuanqi.domain.CollectionRecord;
import com.yuanqi.repository.CollectionRecordRepository;
import com.yuanqi.service.CollectionRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CollectionRecordServiceImpl implements CollectionRecordService {
    
    @Autowired
    private CollectionRecordRepository collectionRecordRepository;
    
    @Override
    public Page<CollectionRecord> getCollectionRecords(Pageable pageable, String collectionCode, String customerName, String collectionDate) {
        if (pageable == null) {
            pageable = PageRequest.of(0, 10);
        }
        
        if (collectionCode != null || customerName != null || collectionDate != null) {
            List<CollectionRecord> records = getCollectionRecordsByConditions(collectionCode, customerName, collectionDate);
            return Page.empty(pageable);
        }
        
        return collectionRecordRepository.findAll(pageable);
    }
    
    @Override
    public CollectionRecord getCollectionRecordById(Integer id) {
        return collectionRecordRepository.findById(id).orElse(null);
    }
    
    @Override
    public CollectionRecord getCollectionRecordByCollectionCode(String collectionCode) {
        return collectionRecordRepository.findByCollectionCode(collectionCode).orElse(null);
    }
    
    @Override
    @Transactional
    public CollectionRecord createCollectionRecord(CollectionRecord collectionRecord) {
        if (collectionRecord.getCollectionCode() == null) {
            collectionRecord.setCollectionCode(generateCollectionCode());
        }
        collectionRecord.setCreateTime(LocalDateTime.now());
        collectionRecord.setUpdateTime(LocalDateTime.now());
        return collectionRecordRepository.save(collectionRecord);
    }
    
    @Override
    @Transactional
    public CollectionRecord updateCollectionRecord(CollectionRecord collectionRecord) {
        collectionRecord.setUpdateTime(LocalDateTime.now());
        return collectionRecordRepository.save(collectionRecord);
    }
    
    @Override
    @Transactional
    public void deleteCollectionRecord(Integer id) {
        collectionRecordRepository.deleteById(id);
    }
    
    @Override
    public String generateCollectionCode() {
        return "SK" + System.currentTimeMillis();
    }
    
    @Override
    public Map<String, Object> getCollectionStats() {
        Map<String, Object> stats = new HashMap<>();
        
        try {
            long totalRecords = collectionRecordRepository.count();
            long pendingRecords = collectionRecordRepository.countByStatus(CollectionRecord.STATUS_PENDING);
            long collectedRecords = collectionRecordRepository.countByStatus(CollectionRecord.STATUS_COLLECTED);
            long cancelledRecords = collectionRecordRepository.countByStatus(CollectionRecord.STATUS_CANCELLED);
            
            stats.put("totalRecords", totalRecords);
            stats.put("pendingRecords", pendingRecords);
            stats.put("collectedRecords", collectedRecords);
            stats.put("cancelledRecords", cancelledRecords);
            
        } catch (Exception e) {
            stats.put("error", "获取统计数据失败：" + e.getMessage());
        }
        
        return stats;
    }
    
    @Override
    public List<CollectionRecord> getCollectionRecordsByConditions(String collectionCode, String customerName, String collectionDate) {
        LocalDate date = null;
        if (collectionDate != null && !collectionDate.isEmpty()) {
            try {
                date = LocalDate.parse(collectionDate);
            } catch (Exception e) {
                // 日期格式错误，忽略
            }
        }
        
        return collectionRecordRepository.findByConditions(collectionCode, customerName, collectionDate);
    }
}




